package com.hawk.eye.client.logback.provider;

import ch.qos.logback.classic.spi.CallerData;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.IThrowableProxy;
import com.fasterxml.jackson.core.JsonGenerator;
import net.logstash.logback.composite.AbstractJsonProvider;

import java.io.IOException;

/**
 * @className: ErrorNameProvider
 * @description: 用于记录发生的异常类名的Provider
 * @author: salad
 * @date: 2024/10/4
 **/
public class ErrorNameProvider extends AbstractJsonProvider<ILoggingEvent> {

    @Override
    public void writeTo(JsonGenerator generator, ILoggingEvent event) throws IOException {
        generator.writeStringField("errorName", errorName(event));
    }

    private String errorName(ILoggingEvent event){
        IThrowableProxy throwableProxy = event.getThrowableProxy();
        if (throwableProxy != null && throwableProxy.getClassName() != null){
            return throwableProxy.getClassName();
        }
        return CallerData.NA;
    }


}